#include"Structt.h"
#include"Menu.h"
#include"function.h"

void analyse(Student* s,int n)//un
{
	double a[n],c[n],d[n];
	memset( a ,0 ,n );
	memset( c ,0 ,n );
	memset( d ,0 ,n );
	
	double h_new[n],h_early[n];
	memset( h_new ,0 ,n );
	memset( h_early ,0 ,n );
		
	double w_new[n],w_early[n];
	memset( w_new ,0 ,n );
	memset( w_early ,0 ,n );
		
	double c_new[n],c_early[n];
	memset( c_new ,0 ,n );
	memset( c_early ,0 ,n );
	
	double h_change[n];
	memset( h_change ,0 ,n );
	
	double w_change[n];
	memset( w_change ,0 ,n );
	
	double c_change[n];
	memset( c_change ,0 ,n );
	
	int count[n];
	memset( count ,0 ,n );
	
	for( int i = 0;i < n && s[i].sno != 0 && strcmp( s[i].gender,"f") == 0;i++){
	
		count[i]++;
		
		for( int m = 0;m < 5 && s[i].cc[m].year !=0 ;m++){
			if( s[i].cc[m].year != s[i].cc[0].year )
			count[i]++;
		}
		
		for( int m = 0;m < 5 && s[i].cc[m].year !=0 ;m++){
		    
			for( int z = 0;z < 5 && s[i].cc[z].year !=0 ;z++){
		    	
				if( s[i].cc[m].year > s[i].cc[z].year ){
		        
					h_new[i] = s[i].cc[m].height;
		        	
		        	w_new[i] = s[i].cc[m].weight;
		        	
		        	c_new[i] = s[i].cc[m].chest_size;
		        	
		        }
		        
		        if( s[i].cc[m].year < s[i].cc[z].year ){
		        	
		        	h_early[i] = s[i].cc[m].height; 
		        	
		        	w_early[i] = s[i].cc[m].weight;
		        	
		        	c_early[i] = s[i].cc[m].chest_size;
		        	
				}
		
	     	}    
			    
		}
		
		h_change[i] = h_new[i] - h_early[i];
			        	
	    w_change[i] = w_new[i] - w_early[i];
	       	
	    c_change[i] = c_new[i] - c_early[i];        
		        			
	}
	
	for( int i = 0;i < n && s[i].sno != 0 && strcmp( s[i].gender,"m") == 0;i++){
	
		count[i]++;
		
		for( int m = 0;m < 5 && s[i].cc[m].year !=0 ;m++){
			if( s[i].cc[m].year != s[i].cc[0].year )
			count[i]++;
		}
		
		for( int m = 0;m < 5 && s[i].cc[m].year !=0 ;m++){
		    
			for( int z = 0;z < 5 && s[i].cc[z].year !=0 ;z++){
		    	
				if( s[i].cc[m].year > s[i].cc[z].year ){
		        
					h_new[i] = s[i].cc[m].height;
		        	
		        	w_new[i] = s[i].cc[m].weight;
		        	
		        	c_new[i] = s[i].cc[m].chest_size;
		        	
		        }
		        
		        if( s[i].cc[m].year < s[i].cc[z].year ){
		        	
		        	h_early[i] = s[i].cc[m].height; 
		        	
		        	w_early[i] = s[i].cc[m].weight;
		        	
		        	c_early[i] = s[i].cc[m].chest_size;
		        	
				}
		
	     	}    
			    
		}
		
		h_change[i] = h_new[i] - h_early[i];
			        	
	    w_change[i] = w_new[i] - w_early[i];
	       	
	    c_change[i] = c_new[i] - c_early[i];        
		        			
	}

	printf("\n\t\t\taver_increaing column");
	printf("\n\n\tgender\taver_in_height\taver_in_weight\taver_in_chest_size");
	
	for( int i =0;i<n&&s[i].sno !=0;i++){
		
	    int b = 0;
	    
	    for( int m = 0;m<n&&s[m].sno !=0&&m!=i;m++){
	    	
	    	if( strcmp( s[i].gender,s[m].gender ) == 0 ) {
	    		b++;
			break ;
			}
			
		}
		
		if(b == 0){
			
			a[i] =  h_change[i]/count[i];
			
			c[i] =  w_change[i]/count[i];
			
			d[i] =  c_change[i]/count[i];
			
			
			
		
		}
		
	}
	
	printf("\n\t%s\t%lf\t%lf\t%lf\n",s[i].gender ,a ,c ,d );
	
	

	for( int i = 0;i < n && s[i].sno != 0 && strcmp( s[i].gender,"m") == 0;i++){
			
		for( int m = 0;m < 5 ;m++){
		
		    for( int z = 0;z < 5 ;z++){
		    	
		        if( s[i].cc[m].year > s[i].cc[z].year ){
		        
		        	h_change[i] = ( s[i].cc[m].height - s[i].cc[z].height )/s[i].cc[z].height  ;
		        	
		        	w_change[i] = ( s[i].cc[m].weight - s[i].cc[z].weight )/s[i].cc[z].weight  ;
		        	
		        	c_change[i] = ( s[i].cc[m].chest_size - s[i].cc[z].chest_size )/s[i].cc[z].chest_size  ;
		        
		        }
		        
		        else {
		        	
		        	h_change[i] = 0 ;
		        
		        	w_change[i] = 0 ;
		        
		        	c_change[i] = 0 ;
		        	
				}
		    
			}
				
		}
		
	}
	
	for( int i = 0;i < n && s[i].sno != 0 && strcmp( s[i].gender,"f") == 0;i++){
			
		for( int m = 0;m < 5 ;m++){
		
		    for( int z = 0;z < 5 ;z++){
		    	
		        if( s[i].cc[m].year > s[i].cc[z].year ){
		        
		        	h_change[i] = ( s[i].cc[m].height - s[i].cc[z].height )/s[i].cc[z].height ;
		        
		        	w_change[i] = ( s[i].cc[m].weight - s[i].cc[z].weight )/s[i].cc[z].weight ;
		        
		        	c_change[i] = ( s[i].cc[m].chest_size - s[i].cc[z].chest_size )/s[i].cc[z].chest_size ;
		        	
		        }
		        
		        else {
		        	
		        	h_change[i] = 0 ;
		        
		        	w_change[i] = 0 ;
		        
		        	c_change[i] = 0 ;
		        	
				}
		    
			}
				
		}
		
	}
	/*
	printf("\n\t\t\taver_increaing rate");
	printf("\n\n\tgender\taver_in_height\taver_in_weight\taver_in_chest_size");
	
	for( int i =0;i<n&&s[i].sno !=0;i++){
		
	    int b = 0;
	    
	    for( int m = 0;m<n&&s[m].sno !=0&&m!=i;m++){
	    	
	    	if( strcmp( s[i].gender,s[m].gender ) == 0 ) {
	    		b++;
			break ;
			}
			
		}
		
		if(b == 0){
			a =  h_change[i]/count[i];
		
			c =  w_change[i]/count[i];
		
			d =  c_change[i]/count[i];
			printf("\n\t%s\t%lf\t%lf\t%lf\n",s[i].gender ,a ,c ,d );
			
		}
	}
	*/
    
}

Student* readFile( Student* s ,int n )  //读 
{
	FILE* fp;
	fopen_s(&fp,"zxcFile","rb");
	if(!fp)return s;
	fread(s,sizeof(Student ),n,fp);
	fclose(fp);
	return s;
}

void saveFile( Student* s,int n ) //写 
{
	FILE* fp;
	fopen_s(&fp,"zxcFile","wb");
		if(!fp){
		fclose(fp);	
		return ;
		}
	fwrite(s,sizeof(Student ),n,fp);
	fclose(fp);
	return ;
}

void statistic(Student* s,int n)
{
	int max[n];
	memset(max,0,n);
	
	int count[n];
	memset(count,0,n);
	
	float aheight[n] ;
	memset(aheight,0,n);
	
	double aweight[n] ;
	memset(aweight,0,n);
	
	double achest_size[n] ;
	memset(achest_size,0,n);
	
	double a_height[n] ;
	memset(a_height,0,n);
	
	double a_weight[n] ; 
	memset(a_weight,0,n);
	
	double a_chest_size[n] ;
	memset(a_chest_size,0,n);
	// max:now age

    for(int i = 0;i < n && s[i].sno != 0;i++){
    		
    	for(int m = 0;m<5;m++){
			
    		for(int z = m;z<5;z++){
    		     	
    			if(s[i].cc[m].age >= s[i].cc[z].age && s[i].cc[z].age!=0 &&s[i].cc[m].age!=0)max[i] =s[i].cc[m].age;
    			
    			if(s[i].cc[m].height >= s[i].cc[z].height && s[i].cc[z].height!=0 &&s[i].cc[m].height!=0)   aheight[i] =  s[i].cc[m].height ;
    		 
    			if(s[i].cc[m].weight >= s[i].cc[z].weight && s[i].cc[z].weight!=0 &&s[i].cc[m].weight!=0)   aweight[i] = s[i].cc[m].weight ;
    			
    			if(s[i].cc[m].chest_size >= s[i].cc[z].chest_size && s[i].cc[z].chest_size!=0 &&s[i].cc[m].chest_size!=0)  achest_size[i] =s[i].cc[m].chest_size ;
    			
    		 }
         }
     }
    	
    // count:the number of the same age
     
   
    for(int i = 0;i<n&&max[i]!=0;i++ ){
    	count[i] = 1;
       
    	for(int m = 0;m<n&&max[m]!=0&&m!=i;m++){
    	
    		if(max[i] == max[m])  {
    			count[i]++;
    			count[m]++;
			}
    		
		}
	}  
	
	for(int i = 0;i<n&&max[i]!=0;i++){
		
		for(int j = 0;j<n&&max[j]!=0;j++){
			    
			if(max[i] == max[j] && strcmp( s[i].gender ,s[j].gender ) == 0 ){
			    a_height[i] += aheight[j];
	            a_weight[i] += aweight[j] ;
	            a_chest_size[i] += achest_size[j];
			
			}
			
		}
		
	}
	 
    printf("\n\tage\tgender\tnumber\taverheight\taverweight\taverchest_size");
        
	for( int i =0;i<n&&max[i]!=0;i++){
		
	    int b = 0;
	    
	    for( int m = 0;m<n&&max[m]!=0&&m!=i;m++){
	    	
	    	if(max[i] == max[m]) {
	    		b++;
			break ;
			}	
			
		}
		
		if(b == 0)
		printf("\n\t%d\t%s\t%d\t%f\t%f\t%f\n",max[i],s[i].gender,count[i],a_height[i]/count[i],a_weight[i]/count[i],a_chest_size[i]/count[i] );		
	
		
	}
		
	
}

void change(Student* s,int n)
{
	int a ;
	printf("输入学号:");
	scanf("%d",&a);
	for(int i = 0;i < n ;i++)
	{
		if(s[i].sno == a)
		input2( &(s[i]) );
		
	}
	
	return ;
}

void del_fir(Student* s,int n)
{
	
	for(int i=0;i<n-1;i++){
	s[i]=s[i+1];
    }
	return ;
	
}

void del_las(Student* s,int n)
{

	int i=0;
	for(;i<n;i++)
	{
		if(s[i+1].sno == 0)break;
		
	}
	s[i]=s[i+1];

	return ;

}

void del_sno(Student* s,int n)
{
	
	int sno;
	printf("输入要删除信息的学生学号：");
	scanf("%d",&sno);
	int i=0;
	for(;i<n;i++){
		if(s[i].sno==sno)break;
		
	}
	for(;i<n;i++){
	s[i]=s[i+1];
	}
	
	return ;
	
}

void add(Student* s,int n)
{
	int i,y;
	while(1){
		system("cls");
		printf("\t1.增加学生档案\n");
		printf("\t2.增加个人病例\n");
		printf("\t0.退出\n");
		printf("\t（0-1）\n");
		int option ;
		scanf("%d",&option);
		fflush(stdin);
		switch(option){
			case 0:
				return ;
				break;
				
		    case 1:
			    printf("\n输入学号：");
				scanf("%d",&y);
				for( i =0;i<n&&s[i].sno != 0;i++){
					
					if(s[i].sno == y){
						
						printf("\n\t已有该学生的信息！\n");
						return ;
						
					}
					
	            }
	            
	            for( i =0;i<n&&s[i].sno != 0;i++){}
	            
	            printf("\n\tsno:");
				scanf("%d",&s[i].sno);
				
				printf("\n\tname:");
				fflush(stdin);
				gets(s[i].name);
				
				
				printf("\n\tgender");
				printf("\n\t输入f(女性)/m（男性）:");
				fflush(stdin);
				gets(s[i].gender);
				input2(&s[i]);
				break;	
				
			case 2:
			    add2( s,n );
				break;
			
			default :
			    continue;	
			    
		}
		
	}
     
}

void add2(Student* s,int n)//cac
{
	int a;
	int m ;
	printf("\n\t输入学号：");
	scanf("%d",&a);
	for(int i = 0;i < n&&s[i].sno != 0;i++){
	
		if( a == s[i].sno ){
			
			for( m = 0;m<5&&s[i].cc[m].age!=0;m++){}
			
			if( m >= 4){
				printf("录入失败，请及时清理！");
				return ;
			}
		
			else input3(& ( s[i].cc[m] ) );	
			
		}
		
	}

}

void input1(Student* s,int n)
{
	int i=0 ;
	int a;
	while(1)
	{
		
		system("cls");
		printf("\t1.录入病例\n");
		printf("\t0.结束录入\n");
		printf("\t(0-1)\n");
		int option ;
		scanf("%d",&option);
		fflush(stdin);
		switch(option){
			case 0:return ;
			
			case 1:
	            for( i =0;i<n;i++){
	            printf("\n\t按0退出\n");
	            scanf("%d",&a);
	            if(a != 0){
	            	printf("\n\tsno:");
					scanf("%d",&s[i].sno);
					
					printf("\n\tname:");
					fflush(stdin);
					gets(s[i].name);
					
					
					printf("\n\tgender");
					printf("\n\t输入f(女性)/m（男性）:");
					fflush(stdin);
					gets(s[i].gender);
					input2(&s[i]);		
				}
				else return;
	            		
				}
	            
				break;
				
			default :
			    continue;	
			    
		}
		 
	}
    
	return ;
}

void input2 (Student* s)
{
	int i = 0 ;
	
	while(1)
	{
		system("cls");
		printf("\t1.录入个人病例\n");
		printf("\t0.结束录入\n");
		printf("\t(0-1)\n");
		int option ;
		scanf("%d",&option);
		switch(option){
			case 0:
				return ;
				break;
			
			case 1:
				if (i>=4){
					printf("录入失败，请及时清理！");
					return ;
				}
				input3(& ( (s->cc)[i] ) );
				i++;
				break;
			
			default :
			    continue;
					
		}
		
	}
    
	return ;
	
}

void input3(Cas* s)
{
	fflush(stdin);
	printf("\n\n\tdepart:");	
	gets(s->depart);
	
	printf("\n\tage:");
	scanf("%d",&s->age);
	
	printf("\n\tchest_size:");
	scanf("%lf",&s->chest_size);
	
	printf("\n\theight:");
	scanf("%lf",&s->height);
	
	printf("\n\tweight:");
	scanf("%lf",&s->weight);
	 
	printf("\n\tdiagnose:");
	fflush(stdin);
    gets(s->diagnose );
     
    printf("\n\tyear:");
    scanf("%d",&s->year );
    
    printf("\n\tmonth:");
    scanf("\t%d",&s->month );
    
	printf("\n\tday:");
    scanf("\t%d",&s->day );
    
    return ;
}

Student* readfile(Student* s,int n)
{
	FILE* fp;
	fopen_s(&fp,"zxc_File","rb");
	if(!fp){
		fclose(fp);
		return s;
	}
	fread(s,sizeof(Student ),n,fp);
	fclose(fp);
	return s;
}

void savefile(Student* s,int n)
{
	FILE* fp;
	fopen_s(&fp,"zxc_File","wb");
	if(!fp){
		fclose(fp);
		return ;
	}
	fwrite(s,sizeof(Student ),n,fp);
	fclose(fp);
	return ;
}

void find_by_sno(Student* s,int n,int sno)
{
	for(int i = 0;i < n;i++){
		if( s[i].sno == sno )show1(&s[i]);
	}
}

void find_by_gender(Student* s,int n,char* gender)
{
	for(int i = 0;i < n;i++){
		if( strcmp(s[i].gender ,gender ) == 0 )show1(&s[i]);
	}
}

void find_by_name(Student* s,int n,char* name)
{
	for(int i = 0;i < n;i++){
		if( strcmp(s[i].name ,name ) == 0 )show1(&s[i]);
	}
}

void show1( Student* s )//一个学生
{
	if(  s->sno == 0 ){
			
			return ;
		}
		
	    printf("\n\tsno:%d",s->sno)	;
		
		printf("\n\tname:");
		puts(s->name);
		
		printf("\tgender:");
		puts(s->gender);	
		
	for(int i=0;i<5;i++){
		if(s->cc[i].age == 0 )return;
		else 
		{
		show2( &((s->cc)[i]) );
		
		printf("\n");
		}
	}
	return ;
}

void traversal(Student* s,int n)//所有学生
{
	for(int i = 0; i < n; i++){
		
		if(s[i].sno == 0) return;
	    show1(&s[i]);	
        
	}
}

void show2(Cas* s)//一份记录
{

	printf("\n\tdepart:");
	puts(s->depart );
	
	printf("\tage:%d\n",s->age );
	
	printf("\theight:%f\n",s->height );
	
	printf("\tweight:%f\n",s->weight );
	
	printf("\tchest_size:%f\n",s->chest_size );
	
	printf("\tdiagnose:");
	puts(s->diagnose );
	
	printf("\tdate:%d.%d.%d",s->year ,s->month ,s->day );
	
	printf("\n");
	return ;
}

void deletE(Student* s,int n)
{
	while(1)
	{
		system("cls");
		printf("\n\t1.删除第一个学生");
		printf("\n\t2.删除最后一个学生");
		printf("\n\t3.删除指定学号的学生");
		printf("\n\t0.退出");
		printf("\n\t(0-3)\n");
		
		int option ;
		scanf("%d",&option);
		switch(option)
		{
			case 0:
				return;
				break;
			
			case 1:
				del_fir( s,n );
				traversal( s,n );
				break;
				
			case 2:
				del_las( s,n );
				traversal( s,n );
				break;
				
			case 3:
				del_sno( s,n );
				traversal( s,n );
				break;
				
			default:
				continue;
				
		}
		
	system("pause");
		
	}
	return ;
}